/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-03-25 22:54:13
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.bp.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.google.common.collect.Lists;
import com.joysuch.wwyt.bp.bean.BpAccidentCorrectiveMeasureDetailBean;
import com.joysuch.wwyt.bp.bean.BpAccidentEditBean;
import com.joysuch.wwyt.bp.bean.BpAccidentEditBean.Detail;
import com.joysuch.wwyt.bp.bean.BpAccidentListBean;
import com.joysuch.wwyt.bp.bean.BpAccidentListBean.SearchBpAccident;
import com.joysuch.wwyt.bp.bean.contractor.BpContractorNameDto;
import com.joysuch.wwyt.bp.entity.BpAccident;
import com.joysuch.wwyt.bp.enums.BpAccidentAuditStates;
import com.joysuch.wwyt.bp.service.BpAccidentService;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bp/accident")
@Api(tags = "/bp/accident 事故管理")
public class BpAccidentController {

    private static final Logger logger = LoggerFactory.getLogger(BpAccidentController.class);

    @Autowired
    private BpAccidentService bpAccidentService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("bp:accident:add")
    @ApiOperation(value = "新增报告")
    @PostMapping("add-fast")
    public Object add(@Valid @RequestBody BpAccidentEditBean entity) {
        bpAccidentService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("bp:accident:update")
    @ApiOperation(value = "修改报告")
    @PostMapping("update-fast")
    public Object updateFast(@Validated @RequestBody BpAccidentEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        //只有待审核和审核未通过可以修改
        if (!(entity.getAuditState() == null
                || entity.getAuditState().equals(BpAccidentAuditStates.TO_AUDIT.getState())
                || entity.getAuditState().equals(BpAccidentAuditStates.AUDIT_FAILURE.getState()))) {
            throw new IllegalArgumentException("事故状态不正确");
        }
        bpAccidentService.update(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("bp:accident:update")
    @ApiOperation(value = "审核")
    @PostMapping("update-detail")
    public Object update(@Validated(value = Detail.class) @RequestBody BpAccidentEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        //审核通过 state变为1 不让删除
        if (entity.getAuditState() != null && entity.getAuditState().equals(BpAccidentAuditStates.AUDIT_SUCESS.getState())) {
            entity.setState((byte) 1);
        }
        bpAccidentService.update(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("bp:accident")
    @RequestMapping("page")
    public Object page(Pageable page, SearchBpAccident condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
        Page<BpAccidentListBean> data = bpAccidentService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("bp:accident")
    @ApiOperation(value = "事故报告页面分页")
    @GetMapping("detail-report-page")
    public Object detailPage(Pageable page, SearchBpAccident condition) {
        page = PageDataUtils.addOrderByDesc(page, "id");
//        condition.setState((byte) 1);
        condition.setAuditStates(Lists.newArrayList(2));
        Page<BpAccidentListBean> data = bpAccidentService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("bp:accident")
    @ApiOperation(value = "新增/审核 页面分页")
    @GetMapping("fast-report-page")
    public Object fastPage(Pageable page, SearchBpAccident condition) {
        Page<BpAccidentListBean> data = bpAccidentService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("bp:accident")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchBpAccident condition) {
        // 只查询发起了详细报告的事故
//        condition.setState((byte) 1);
        //只查看审核通过的
        condition.setAuditStates(Lists.newArrayList(2));
        Page<BpAccidentListBean> data = bpAccidentService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(BpAccident.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("bp:accident:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        bpAccidentService.delete(id);
        return ResultBean.success("");
    }

    @RequiresPermissions("bp:accident")
    @GetMapping("get")
    @ApiOperation(value = "查看")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(bpAccidentService.getById(id));
    }

    @PostMapping("set-measure")
    @RequiresPermissions("bp:accident:set-measure")
    @ApiOperation("修改事故发生项")
    public ResultBean setMeasure(@RequestBody BpAccidentCorrectiveMeasureDetailBean bean) {
        return bpAccidentService.setMeasure(bean);
    }
}
